<template>
  <div class="admin">
    <div class="doMessage">
      <div class="search">
        <input type="text" placeholder="请输入关键字" v-model="name" />
        <button @click="search(1)">搜索</button>
      </div>
      
    </div>
    <!-- 表格 -->
    <el-table :data="tableData" style="width: 100%">
      <el-table-column label="菜谱编号" prop="_id"> </el-table-column>
      <el-table-column label="名称" prop="name">
        <!-- <img :src="scope.row.image_src" alt=""> -->
      </el-table-column>

      <el-table-column label="缩略图">
        <template slot-scope="scope">
          <img :src="scope.row.coverpic" alt="" width="40px" height="40px" />
        </template>
      </el-table-column>
      <el-table-column label="浏览量" prop="pageview"> </el-table-column>
      <el-table-column label="收藏量" prop="collections"> </el-table-column>
      <el-table-column label="是否免费" prop="isFree">
        <template slot-scope="scope">
          <span>{{scope.row.isFree | yesOrNoFilter}}</span>
        </template>
         </el-table-column>
      <el-table-column label="是否热点" prop="isHot">
        <template slot-scope="scope">
          <span>{{scope.row.isHot | yesOrNoFilter}}</span>
        </template>
         </el-table-column>
      <el-table-column align="center">
        <template slot="header" slot-scope="scope"> 原料 </template>
        <template slot-scope="scope">
          <span @click="look(scope.row)">查看</span>
          <el-dialog title="原料" :visible.sync="dialogTableVisible">
            <el-table :data="gridData">
              <el-table-column
                property="name"
                label="用料"
                width="150"
              ></el-table-column>
              <el-table-column
                property="number"
                label="用量"
                width="200"
              ></el-table-column>
            </el-table>
          </el-dialog>
        </template>
      </el-table-column>
      <el-table-column align="center">
        <template slot="header" slot-scope="scope"> 制作步骤 </template>
        <template slot-scope="scope">
          <span @click="toMethod(scope.row)">查看</span>
        </template>
      </el-table-column>
      <el-table-column align="center">
        <template slot="header" slot-scope="scope">
         
          操作
        </template>
        <template slot-scope="scope">
          
          <el-button size="mini" type="primary" @click="update(scope.row)"
            >修改</el-button
          >

          <el-dialog title="修改菜品" :visible.sync="dialogFormVisible1">
            <el-form :model="form1">
              <el-form-item label="菜品id" :label-width="formLabelWidth">
                <el-input v-model="form1._id" autocomplete="off"></el-input>
              </el-form-item>
              <el-form-item label="是否免费" :label-width="formLabelWidth">
                <el-input v-model="form1.isFree" autocomplete="off"></el-input>
              </el-form-item>
              <el-form-item label="是否热点" :label-width="formLabelWidth">
                <el-input v-model="form1.isHot" autocomplete="off"></el-input>
              </el-form-item>
              <el-form-item label="名字" :label-width="formLabelWidth">
                <el-input v-model="form1.name" autocomplete="off"></el-input>
              </el-form-item>
              <el-form-item label="时间" :label-width="formLabelWidth">
                <el-input
                  v-model="form1.needtime"
                  autocomplete="off"
                ></el-input>
              </el-form-item>
              
            </el-form>
            <div slot="footer" class="dialog-footer">
              <el-button @click="dialogFormVisible1 = false">取 消</el-button>
              <el-button type="primary" @click="add1">确 定</el-button>
            </div>
          </el-dialog>
        </template>
      </el-table-column>
    </el-table>
    <!-- 分页 -->

    <div class="block">
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="currentPage"
        :page-sizes="[4, 8, 12, 16]"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
      >
      </el-pagination>
    </div>
  </div>
</template>

<script>

export default {
  data() {
    return {
      tableData: [],
      gridData: [],
      name: "",
      searchData: [],
      name1: false,
      falg: false,
      dialogFormVisible: false,
      dialogFormVisible1: false,
      dialogTableVisible: false,
      form: {
        goods_id: "",
        image_src: "",
        navigator_url: "",
        open_type: "",
      },
      form1: {
        _id: "",
        isFree: "",
        isHot: "",
        name: "",
        needtime: "",
      },
      formLabelWidth: "120px",
      total: null,
      pageSize: 4,
      currentPage: 1,
    };
  },
  methods: {
    // handleEdit(index, row) {
    //   console.log(index, row);
    // },
    handleSizeChange(val) {
      this.pageSize = val;
      this.search();
    },
    handleCurrentChange(val) {
      this.currentPage = val;
      this.search();
      // this.falg=false
    },
    async handleDelete(row) {
      let obj = {
        _id: row._id,
      };
      await this.$http.contourMap.deleteSwiper(obj);
      this.getAdminApi();
    },

    async getAdminApi() {
      let { data: res } = await this.$http.foods.findAllMenu({
        pageSize: this.pageSize,
        currentPage: this.currentPage,
      });
      console.log(res);
      this.tableData = res.menus;

      if (!this.total) {
        this.total = res.total;
      }
    },
    async search(currentPage) {
      // this.falg=true
      this.name1 = this.name;
      if (currentPage) this.currentPage = currentPage;
      // if (this.falg) {
      //   this.currentPage=1

      // }
      // let newData = this.tableData.filter((item) => item.name.includes(this.name));
      // this.searchData = newData;
      let obj = {
        pageSize: this.pageSize,
        currentPage: this.currentPage,
        val: this.name,
      };
      let newData = await this.$http.foods.searchMenu(obj);
      console.log(newData);
      // if (newData.data.menus.length > 0) {

      //   this.tableData = newData.data.menus;
      //   this.total = newData.data.total;

      // } else {
      //   this.currentPage = 1;

      //   obj = {
      //     pageSize: this.pageSize,
      //     currentPage: this.currentPage,
      //     val: this.name,
      //   };

      //   newData = await this.$http.foods.searchMenu(obj);
      //   this.tableData = newData.data.menus;
      //   this.total = newData.data.total;
      // }
      this.tableData = newData.data.menus;
      this.total = newData.data.total;
    },
   
    async add1() {
      let { data: res } = await this.$http.foods.updateMenu(this.form1);
      this.dialogFormVisible1 = false;
      this.getAdminApi();
    },
    update(val) {
      console.log(val);
      this.form1 = {
        _id: val._id,
        isFree: val.isFree,
        isHot: val.isHot,
        name: val.name,
        needtime: val.needtime,
      };
      this.dialogFormVisible1 = true;
    },
    look(val) {
      // console.log(val);
      this.gridData = val.ingredient;
      this.dialogTableVisible = true;
    },
    toMethod(val) {
      // this.$router.push("/home/foodsMethod")
      console.log(val);
      this.$router.push({ path: "/home/foodsMethod", query:val });
    },
  },
  created() {
    this.getAdminApi();
  },
  
};
</script>

<style lang='scss' scoped>
.admin {
  padding: 20px 20px 0 20px;
  box-sizing: border-box;
}
.doMessage {
  display: flex;

  .search {
    width: 300px;
    height: 40px;
    display: flex;
    margin-right: 50px;
    input {
      flex-grow: 1;
    }
    button {
      width: 50px;
      border: none;
      outline: none;
      background-color: blue;
      color: white;
    }
  }
  .add {
    width: 80px;
    height: 40px;
    & > button {
      width: 100%;
      height: 100%;
      border: none;
      outline: none;
      background-color: blue;
      color: white;
    }
  }
}
</style>